package com.concurrent.art.chapter7;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @Author: LIHU
 * @Date: 2019/10/10 16:46
 * @Description:
 */
public class AtomicIntegerFieldUpdaterTest {

    private static AtomicIntegerFieldUpdater<User> a = AtomicIntegerFieldUpdater.newUpdater(User.class, "old");

    public static void main(String[] args) {
        //设置柯南的年龄10岁
        User conan = new User("conan", 10);
        //柯南长了一岁，但仍然会输出旧的年龄
        System.out.println(a.getAndIncrement(conan));
        //输出柯南现在的年龄
        System.out.println(a.get(conan));

    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class User {
        private String name;
        public volatile int old;
    }
}
